﻿using Client.Todo.Common;
using Client.Todo.Common.Models;
using Client.Todo.Views.Dialogs;
using Common.Lib.Dto;
using Common.Lib.Parameter;
using Common.Lib.Service;
using Prism.Commands;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Client.Common.Extensions;
using Client.Todo.Extendsions;

namespace Client.Todo.ViewModels
{
    /// <summary>
    /// 首页VM
    /// </summary>
    public class IndexViewModel:NavigationViewModel
    {
        public IndexViewModel(IContainerProvider containerProvider,
            IToDoService toDoService,
            IMemoService memoService,
            IDialogHostService dialogService):base(containerProvider)
        {
            this.containerProvider = containerProvider;
            this.toDoService = toDoService;
            this.memoService = memoService;
            this.dialogService = dialogService;
            //InitValue();
            CreateDtos();
        }

        #region 添加待办

        /// <summary>
        /// 添加待办 - 命令
        /// </summary>
        private DelegateCommand _addToDoCmd;

        /// <summary>
        /// 添加待办 - 命令
        /// </summary>
        public DelegateCommand AddToDoCmd =>
            _addToDoCmd ?? (_addToDoCmd = new DelegateCommand(ExecuteAddToDo));

        /// <summary>
        /// 执行 - 添加待办
        /// </summary>    
        async void ExecuteAddToDo()
        {
            var dialogRes = await dialogService.ShowDialog(nameof(AddToDoView),null);
            if(dialogRes.Result == ButtonResult.OK)
            {
                var todo = dialogRes.Parameters.GetValue<ToDoDto>("Value");
                ToDoDtos.Insert(0, todo);
            }
        }

        #endregion 添加待办

        #region 统计信息

        /// <summary>
        /// 统计信息
        /// </summary>
        private SummaryDto _summaryData;

        /// <summary>
        /// 统计信息
        /// </summary>
        public SummaryDto SummaryData
        {
            get { return _summaryData; }
            set { SetProperty(ref _summaryData, value); }
        }

        #endregion 统计信息 

        #region 编辑备忘

        /// <summary>
        /// 编辑备忘 - 命令
        /// </summary>
        private DelegateCommand<MemoDto> _editMemoCmd;

        /// <summary>
        /// 编辑备忘 - 命令
        /// </summary>
        public DelegateCommand<MemoDto> EditMemoCmd =>
            _editMemoCmd ?? (_editMemoCmd = new DelegateCommand<MemoDto>(ExecuteEditMemo));

        /// <summary>
        /// 执行 - 编辑备忘
        /// </summary>    
        async void ExecuteEditMemo(MemoDto parameter)
        {
            if(parameter != null)
            {
                var val = new DialogParameters();
                val.Add("Value", parameter);
                var dialogRes = await dialogService.ShowDialog(nameof(AddMemoView), val);
                if (dialogRes.Result == ButtonResult.OK)
                {
                    var memo = dialogRes.Parameters.GetValue<MemoDto>("Value");
                    var index = MemoDtos.IndexOf(parameter);
                    MemoDtos[index] = memo;
                }
            }
        }

        #endregion 编辑备忘

        #region ToDo状态改变

        /// <summary>
        /// ToDo状态改变 - 命令
        /// </summary>
        private DelegateCommand<ToDoDto> _toDoStatusChangedCmd;

        /// <summary>
        /// ToDo状态改变 - 命令
        /// </summary>
        public DelegateCommand<ToDoDto> ToDoStatusChangedCmd =>
            _toDoStatusChangedCmd ?? (_toDoStatusChangedCmd = new DelegateCommand<ToDoDto>(ExecuteToDoStatusChanged));

        /// <summary>
        /// 执行 - ToDo状态改变
        /// </summary>    
        async void ExecuteToDoStatusChanged(ToDoDto parameter)
        {
            if (parameter != null)
            {
                parameter.Status = 1;
                var res = await toDoService.UpdateAsync(parameter);
                if (res?.Status == true)
                {
                    var dto = ToDoDtos.FirstOrDefault(p => p.Id == parameter.Id);
                    if (dto != null)
                    {
                        ToDoDtos.Remove(dto);
                    }
                }
            }
        }

        #endregion ToDo状态改变



        #region 编辑待办

        /// <summary>
        /// 编辑待办 - 命令
        /// </summary>
        private DelegateCommand<ToDoDto> _editToDoCmd;

        /// <summary>
        /// 编辑待办 - 命令
        /// </summary>
        public DelegateCommand<ToDoDto> EditToDoCmd =>
            _editToDoCmd ?? (_editToDoCmd = new DelegateCommand<ToDoDto>(ExecuteEditToDo));

        /// <summary>
        /// 执行 - 编辑待办
        /// </summary>    
        async void ExecuteEditToDo(ToDoDto parameter)
        {
            if (parameter != null)
            {
                var val = new DialogParameters();
                val.Add("Value", parameter);
                var dialogRes = await dialogService.ShowDialog(nameof(AddToDoView), val);
                if (dialogRes.Result == ButtonResult.OK)
                {
                    var todo = dialogRes.Parameters.GetValue<ToDoDto>("Value");
                    var index = ToDoDtos.IndexOf(parameter);
                    ToDoDtos[index] = todo; ;
                }
            }
        }

        #endregion 编辑待办

        #region 添加备注

        /// <summary>
        /// 添加备注 - 命令
        /// </summary>
        private DelegateCommand _addMemoCmd;

        /// <summary>
        /// 添加备注 - 命令
        /// </summary>
        public DelegateCommand AddMemoCmd =>
            _addMemoCmd ?? (_addMemoCmd = new DelegateCommand(ExecuteAddMemo));

        /// <summary>
        /// 执行 - 添加备注
        /// </summary>    
        async void ExecuteAddMemo()
        {
            var dialogRes = await dialogService.ShowDialog(nameof(AddMemoView), null);
            if (dialogRes.Result == ButtonResult.OK)
            {
                var memo = dialogRes.Parameters.GetValue<MemoDto>("Value");
                MemoDtos.Insert(0, memo);
            }
        }

        #endregion 添加备注

        #region 任务列表

        /// <summary>
        /// 任务列表
        /// </summary>
        private ObservableCollection<TaskBar> _taskBars;

        /// <summary>
        /// 任务列表
        /// </summary>
        public ObservableCollection<TaskBar> TaskBars
        {
            get { return _taskBars; }
            set { SetProperty(ref _taskBars, value); }
        }
        #endregion 任务列表 

        #region 待办列表

        /// <summary>
        /// 待办列表
        /// </summary>
        private ObservableCollection<ToDoDto> _todoDtos;

        /// <summary>
        /// 待办列表
        /// </summary>
        public ObservableCollection<ToDoDto> ToDoDtos
        {
            get { return _todoDtos; }
            set { SetProperty(ref _todoDtos, value); }
        }

        #endregion 待办列表 

        #region 备忘录列表

        /// <summary>
        /// 备忘录列表
        /// </summary>
        private ObservableCollection<MemoDto> _memoDtos;
        private readonly IContainerProvider containerProvider;
        private readonly IToDoService toDoService;
        private readonly IMemoService memoService;
        private readonly IDialogHostService dialogService;

        /// <summary>
        /// 备忘录列表
        /// </summary>
        public ObservableCollection<MemoDto> MemoDtos
        {
            get { return _memoDtos; }
            set { SetProperty(ref _memoDtos, value); }
        }

        #endregion 备忘录列表 
        void InitValue()
        {
            TaskBars = new ObservableCollection<TaskBar>()
            {
                new TaskBar()
                {
                    Title = "汇总",
                    Icon  = "Sigma",
                    Color = "#00b844",
                    Content = "9",
                    NameSpace = "",
                },
                new TaskBar()
                {
                    Title = "已完成",
                    Icon  = "TimerSandComplete",
                    Color = "#2962ff",
                    Content = "9",
                    NameSpace = "",
                },
                new TaskBar()
                {
                    Title = "完成比例",
                    Icon  = "ChartLineVariant",
                    Color = "#e66025",
                    Content = "100%",
                    NameSpace = "",
                },
                new TaskBar()
                {
                    Title = "备忘录",
                    Icon  = "Collage",
                    Color = "#d50000",
                    Content = "10",
                    NameSpace = "",
                },
            };
        }

        async void CreateDtos()
        {
            ToDoDtos = new ObservableCollection<ToDoDto>();
            MemoDtos = new ObservableCollection<MemoDto>();
            #region Dto数据创建
            //for (int i = 0; i < 10; i++)
            //{
            //    ToDoDtos.Add(new ToDoDto()
            //    {
            //        Id = i,
            //        Title = $"待办{i + 1}",
            //        Content = $"这是待办 {i+1} 的内容",
            //        Status = 0,
            //        CreateTime = DateTime.Now,
            //        UpdateTime = DateTime.Now,
            //    });
            //    MemoDtos.Add(new MemoDto()
            //    {
            //        Id = i,
            //        Title = $"备忘{i + 1}",
            //        Content = $"这是备忘 {i + 1} 的内容",
            //        Status = 0,
            //        CreateTime = DateTime.Now,
            //        UpdateTime = DateTime.Now,
            //    });
            //}
            #endregion Dto数据创建 
            UpdateLoading(true);
            try
            {
                var summaryRes = await toDoService.Summary();
                if(summaryRes?.Status == true)
                {
                    ToDoDtos.AddRange(summaryRes.Result.TodoList);
                    MemoDtos.AddRange(summaryRes.Result.MemoList);
                }
                TaskBars = new ObservableCollection<TaskBar>()
                {
                    new TaskBar()
                    {
                        Title = "汇总",
                        Icon  = "Sigma",
                        Color = "#00b844",
                        Content = summaryRes.Result.Sum.ToString(),
                        NameSpace = "",
                    },
                    new TaskBar()
                    {
                        Title = "已完成",
                        Icon  = "TimerSandComplete",
                        Color = "#2962ff",
                        Content = summaryRes.Result.CompletedCount.ToString(),
                        NameSpace = "",
                    },
                    new TaskBar()
                    {
                        Title = "完成比例",
                        Icon  = "ChartLineVariant",
                        Color = "#e66025",
                        Content = summaryRes.Result.CompletedRatio.ToString(),
                        NameSpace = "",
                    },
                    new TaskBar()
                    {
                        Title = "备忘录",
                        Icon  = "Collage",
                        Color = "#d50000",
                        Content = summaryRes.Result.MemoCount.ToString(),
                        NameSpace = "",
                    },
                };

            }
            catch(Exception ex)
            {
                await dialogService.Question(content: ex.Message);
            }
            finally
            {
                UpdateLoading(false);
            }
        }
    }
}
